new Vue({
  el: "#app",
  data() {
    return {
      tc: {
        name: "",
        people: "",
      },
      multipleSelection: [],
      dialogFormVisible: false,
      formLabelWidth: "120px",
      Open_ID: "",
      iJSONArray_member_info: [],
      options: [
        {
          value: "3",
          label: "签收",
        },
        {
          value: "2",
          label: "发货",
        },
        
        {
          value: "0",
          label: "未发货",
        },
      ],
      value: "",
      hdmc: "",
      Activity_SerialNumber: "",
      Article_SerialNumber: "",
      iJSONArray_article_info: [],
      iJSONArray_hdmc: [],
      form: {
        value: "",
        phone: "",
        SQLSerialNumber: "P_business_sales_order_detail3",
        Condition: "",
        ExtensionCondition: "",
        Department_SerialNumber: [],
        Sales_Order_SerialNumber: "",
        Sales_Date: null,
        pageSize: 20,
        pageNumber: 1,
      },
      rules: {
        Department_SerialNumber: [
          { required: true, message: "请选择甲方", trigger: "change" },
        ],
      },
      department: [],
      tableData: [], //表格数据
      loading: false,
      table: true,
    };
  },
  mounted() {
    this.getData();
    const timer = setTimeout(() => {
      this.getColor();
      clearTimeout(timer)
    })
  },
  computed: {
    Type() {
      return function (str) {
        switch (str) {
          case "0":
            return "待发货";
          case "1":
            return "发货中";
          case "2":
            return "已发货";
          default:
            return "";
        }
      };
    },
  },
  methods: {
    getColor() {
      const elements = document.querySelectorAll('.el-descriptions-item__label');
      // 遍历每个元素
      for (let i = 0; i < elements.length; i++) {
        // 获取元素的innerHTML
        let html = elements[i].innerHTML;
        // 使用正则表达式替换所有的 * 为红色的 *
        html = html.replace(/\*/g, '<span style="color: red;">*</span>');
        // 将修改后的HTML重新赋值给元素
        elements[i].innerHTML = html;
      }
      const elements1 = document.querySelectorAll('.el-form-item__label');
      // 遍历每个元素
      for (let i = 0; i < elements1.length; i++) {
        // 获取元素的innerHTML
        let html = elements1[i].innerHTML;
        // 使用正则表达式替换所有的 * 为红色的 *
        html = html.replace(/\*/g, '<span style="color: red;">*</span>');
        // 将修改后的HTML重新赋值给元素
        elements1[i].innerHTML = html;
      }

    },
    qd() {
      dialogFormVisible = false;
      console.log(this.multipleSelection);
      this.multipleSelection[0].name = this.tc.name;
      this.multipleSelection[0].people = this.tc.people;
      const _this = this;
      $.ajax({
        headers: {
          AccessToken: getCookie("token"),
        },
        type: "post",
        contentType: "application/json", // 设置请求体的媒体类型为application/json
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        // url: "https://service.kdddy.cn/hnx/ADD/PurchaseByYi",
        url: "https://service.kdddy.cn/hnx/add/ReconciliationByJia",
        data: JSON.stringify(this.multipleSelection),
        success: function (Result) {
          console.log(Result);
          if (Result.Status === "success") {
            _this.$message.success("提交成功");
            window.location.href = 'index.html';
          } else {
            _this.$message.error("提交失败，请稍后再试");
          }
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    handleSelectionChange(row) {
      this.multipleSelection = row;
    },
    JFhandleChange(value) {
      console.log(value);
      const _this = this;
      // return;
      $.ajax({
        headers: requestHeaders(),
        type: "get",
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/hnx/getActivity/ByJia",
        data: {
          id: value, //表名
        },
        success: function (Result) {
          console.log(Result);
          _this.iJSONArray_hdmc = Result.Data;
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
      $.ajax({
        headers: requestHeaders(),
        type: "get",
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/hnx/getArticle/ByJiaActivity",
        data: {
          id: value, //表名
          aid: "",
        },
        success: function (Result) {
          console.log(Result);
          _this.iJSONArray_article_info = Result.Data;
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
      $.ajax({
        headers: requestHeaders(),
        type: "post",
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service2.essp.top/hnx/generalBusiness/generalQueryByTableName",
        // url: "http://192.168.10.3:8082/generalBusiness/generalQueryByTableName",

        data: {
          Data: "business_member_info", //表名
          Condition:
            "Row_Status='0' And Department_JSON LIKE '%" + value + "%'",
        },
        success: function (Result) {
          console.log(Result);
          _this.iJSONArray_member_info = Result;
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    handleChange(value) {
      console.log(value, "value");
      console.log(this.form.Department_SerialNumber);

      const _this = this;
      // return;
      $.ajax({
        headers: requestHeaders(),
        type: "get",
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/hnx/getArticle/ByJiaActivity",
        data: {
          id: this.form.Department_SerialNumber,
          aid: value, //表名
        },
        success: function (Result) {
          console.log(Result);
          _this.iJSONArray_article_info = Result.Data;
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    // hdmc() {},
    onSerach() {
      console.log(this.form);
      this.form.Row_Status = this.value;
      this.form.start = this.form.Sales_Date[0];
      this.form.end = this.form.Sales_Date[1];
      this.form.Activity_SerialNumber = this.Activity_SerialNumber;
      this.form.Article_SerialNumber = this.Article_SerialNumber;
      this.form.Member_SerialNumber = this.Open_ID;

      let lArray = new Array();
      lArray.push(this.form);
      console.log(lArray);

      // return;
      const _this = this;
      $.ajax({
        // headers: requestHeaders(),
        // headers: {
        //   AccessToken: getCookie("token"),
        // },
        contentType: "application/json", // 设置请求体的媒体类型为application/json
        type: "post",
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        // url: "https://service.kdddy.cn/hnx/get/Reconciliation",
        //500
        url: "https://service.kdddy.cn/hnx/get/Reconciliation",
        // url: "http://192.168.10.134:8088/get/Reconciliation",
        data: JSON.stringify(lArray),
        success: function (Result) {
          console.log(Result);
          _this.tableData = Result.Data;
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
      // this.$refs[formName].validate((valid) => {
      //   if (valid) {
      //     this.form.page = 1;
      //     this.getList(this.form);
      //   } else {
      //     this.$message({
      //       message: "请选择甲方",
      //       type: "warning",
      //     });
      //     return false;
      //   }
      // });
    },
    handleSizeChange(val) {
      this.form.PaginationNumber = 1;
      this.form.RowAmount = val;
    },
    handleCurrentChange(val) {
      this.form.PaginationNumber = val;
    },
    getData() {
      const _this = this;
      $.ajax({
        headers: requestHeaders(),
        type: "POST",
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service2.essp.top/hnx/generalBusiness/generalQueryByTableName",
        data: {
          Data: "business_department_info", //表名
          Condition: "Row_Status='0' And Level='1'",
        },
        success: function (Result) {
          console.log(Result);
          _this.department = Result;
          //that.total = Result.TotalRows;
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });

      // $.ajax({
      //   headers: requestHeaders(),
      //   url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumber`,
      //   type: "POST",
      //   data: {
      //     SQLSerialNumber: "business_department_info",
      //     Condition: "",
      //     ExtensionCondition: "",
      //   },
      //   success: function (res) {
      //     that.department = res;
      //   },
      //   complete: function (XMLHttpRequest) {
      //     requestPretreatment(XMLHttpRequest);
      //   },
      //   dataType: "json",
      // });
    },
    submitForm() {
      if (this.multipleSelection.length > 0) {
        this.dialogFormVisible = true;
      } else {
        this.$message({
          message: "请选择要对账的订单",
          type: "warning",
        });
      }

      // const that = this;
      // if (this.tableData.length > 0) {
      //   let arr = [];
      //   let sql = "";
      //   this.tableData.forEach((item) => {
      //     if (sql) {
      //       sql =
      //         sql +
      //         "or Sales_Order_Detail_SerialNumber='" +
      //         item.Sales_Order_Detail_SerialNumber +
      //         "' \r\n";
      //     } else {
      //       sql =
      //         "Sales_Order_Detail_SerialNumber='" +
      //         item.Sales_Order_Detail_SerialNumber +
      //         "' \r\n";
      //     }
      //     //修改状态
      //     let Reconciliation_Status = item.Reconciliation_Status.charAt(0);
      //     item.Reconciliation_Status = Reconciliation_Status + "1";
      //     let is = true;
      //     for (let i = 0; i < arr.length; i++) {
      //       let obj = arr[i];
      //       if (obj.Department_SerialNumber == item.Department_SerialNumber) {
      //         obj.Sales_Amount = accAdd(obj.Sales_Amount, item.Amount);
      //         obj.Remain_Amount = accAdd(obj.Remain_Amount, item.Amount);
      //         obj.Children.business_department_reconciliation_detail.push({
      //           Department_Reconciliation_Detail_SerialNumber: "",
      //           Department_Reconciliation_SerialNumber: "",
      //           Sales_Order_Detail_SerialNumber:
      //             item.Sales_Order_Detail_SerialNumber,
      //         });
      //         is = false;
      //         break;
      //       }
      //     }
      //     if (is) {
      //       //新
      //       arr.push({
      //         Department_Reconciliation_SerialNumber: "",
      //         Department_SerialNumber: item.Department_SerialNumber,
      //         Submit_Date: getYMDHMSDate(),
      //         Sales_Amount: item.Amount,
      //         Invoice_Amount: 0,
      //         Remain_Amount: item.Amount,
      //         Row_Status: "0",
      //         Children: {
      //           business_department_reconciliation_detail: [
      //             {
      //               Department_Reconciliation_Detail_SerialNumber: "",
      //               Department_Reconciliation_SerialNumber: "",
      //               Sales_Order_Detail_SerialNumber:
      //                 item.Sales_Order_Detail_SerialNumber,
      //             },
      //           ],
      //         },
      //       });
      //     }
      //   });
      //   $.ajax({
      //     headers: requestHeaders(),
      //     type: "POST",
      //     url: `${BASE_URL2}/generalBusiness/generalQueryByTableNameWithOriginal`,
      //     data: {
      //       Data: "business_sales_order_detail",
      //       Condition: sql,
      //     },
      //     success: function (data) {
      //       data.forEach((item) => {
      //         let Reconciliation_Status = item.Reconciliation_Status.charAt(0);
      //         item.Reconciliation_Status = Reconciliation_Status + "1";
      //       });
      //       $.ajax({
      //         headers: requestHeaders(),
      //         url: `${BASE_URL2}/generalBusiness/generalSave`,
      //         type: "POST",
      //         data: {
      //           Data: JSON.stringify({
      //             Add: { business_department_reconciliation: arr },
      //             Modify: { business_sales_order_detail: data },
      //           }),
      //         },
      //         success: function (res) {
      //           if (res.Status === "success") {
      //             that.$message.success("提交成功");
      //             window.history.go(-1);
      //           } else {
      //             that.$message.error("提交失败，请稍后再试");
      //           }
      //         },
      //         complete: function (XMLHttpRequest) {
      //           that.loading = false;
      //           requestPretreatment(XMLHttpRequest);
      //         },
      //         dataType: "json",
      //       });
      //     },
      //     complete: function (XMLHttpRequest) {
      //       requestPretreatment(XMLHttpRequest);
      //     },
      //     dataType: "json",
      //   });
      // } else {
      //   that.$message({
      //     message: "当前没有需要对账的订单明细",
      //     type: "warning",
      //   });
      // }
    },
    getList(form) {
      //换页方法
      const that = this;
      let data = deepClone(form);
      let Sales_Date = ["", ""];
      if (data.Sales_Date) {
        Sales_Date = data.Sales_Date;
      }
      data.Condition = window.btoa(
        unescape(
          encodeURIComponent(
            "%" +
            data.Sales_Order_SerialNumber +
            "%,%" +
            data.Sales_Order_SerialNumber +
            "%"
          )
        )
      );
      let sql = "";
      this.form.Department_SerialNumber.forEach((item) => {
        if (sql) {
          sql =
            sql +
            " or business_department_info.Department_SerialNumber = '" +
            item +
            "'\r\n ";
        } else {
          sql =
            sql +
            " business_department_info.Department_SerialNumber = '" +
            item +
            "'\r\n ";
        }
      });
      data.ExtensionCondition = window.btoa(
        unescape(encodeURIComponent(`and (${sql})`))
      );
      $.ajax({
        headers: requestHeaders(),
        url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumberWithOriginal`,
        type: "POST",
        data,
        success: function (Result) {
          Result.forEach((item) => {
            let arr = item.Department_Name.split("/");
            item.Department = [];
            arr.forEach((i) => {
              item.Department.push({
                Department_Name: i.split("_")[0],
                Department_SerialNumber: i.split("_")[1],
              });
            });
            item.Department_SerialNumber =
              item.Department[0].Department_SerialNumber;
          });
          console.log(Result);
          that.tableData = Result;
          that.table = false;
          const timer = setTimeout(() => {
            that.table = true;
            clearTimeout(timer)
          })
          if (Result.length == 0) {
            that.$message({
              message: "未查询到可对账的订单明细",
              type: "warning",
            });
          }
        },
        complete: function (XMLHttpRequest) {
          requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    handleRemove(index) {
      this.$confirm("是否要删除此行?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.tableData = this.tableData.filter(
            (i, id) => id != (this.form.page - 1) * this.form.limit + index
          );
        })
        .catch(() => { });
    },
  },
});
